package com.example.exercicio01;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Exercicio01 extends ActionBarActivity {
	
	private int calcResult;
	private TextView result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exercicio01);
		
		result = ( TextView )findViewById( R.id.txtResult );
		
		if( savedInstanceState != null ){
			calcResult = savedInstanceState.getInt("resultSave");
			
			//pegar string que esta declarada em STRINGS.XML
			result.setText( getString( R.string.str_result, calcResult ) );
			
		}
	}
	
	public void calc(View v) {
		
		//recupera o valor da caixa de texto 1
		EditText edtText1 = ( EditText )findViewById( R.id.editValue1 );
		
		//recupera o valor da caixa de texto 2
		EditText edtText2 = ( EditText )findViewById( R.id.editValue2 );
		
		int x = Integer.parseInt( edtText1.getText().toString() );
		int y = Integer.parseInt( edtText2.getText().toString() );
		
		if( v.getId() == R.id.btnSum){
			calcResult = x + y;
			
			}else{
				calcResult = x * y;
				}
		
		
		//pegar string que esta declarada em STRINGS.XML
		result.setText( getString( R.string.str_result, calcResult ) );
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.exercicio01, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("resultSave", calcResult);
	}
}
